# 队列：FIFO先进先出


class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None
        self.prev = None


class FIFO:
    def __init__(self):
        self.head = Node()
        self.tail = Node()
        self.length = 0
        self._full_length = 10

        self.head.next = self.tail
        self.tail.prev = self.head

    def full(self):
        if self.length == self._full_length:
            return "full"

    def empty(self):
        if self.length == 0:
            return "empty"

    def unshift(self, data):
        assert not self.full()
        newnode = Node(data)
        prenode = self.tail.prev

        prenode.next = newnode
        newnode.prev = prenode

        newnode.next = self.tail
        self.tail.prev = newnode

        self.length += 1

    def shift(self):
        assert not self.empty()
        prevnode = self.head
        shiftnode = self.head.next
        nextnode = shiftnode.next

        prevnode.next = nextnode
        nextnode.prev = prevnode

        self.length -= 1
        return shiftnode.data

    def __len__(self):
        return self.length

    def __iter__(self):
        node = self.head.next
        while node != self.tail:
            yield node.data
            node = node.next


if __name__ == "__main__":
    l = FIFO()
    for i in range(8):
        l.unshift(i)
    for v in l:
        print(v)

    for i in range(8):
        print("shift data is {}".format(l.shift()))






